package cn.anxingongyou.anxin.controller;

import cn.anxingongyou.anxin.entity.Notice;
import cn.anxingongyou.anxin.entity.Taxi;
import cn.anxingongyou.anxin.service.NoticeService;
import cn.anxingongyou.anxin.service.TaxiService;
import cn.anxingongyou.anxin.utils.RedisUtil;
import cn.anxingongyou.anxin.utils.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class TaxiController {

    private final TaxiService taxiService;
    @Autowired
    public TaxiController(TaxiService taxiService) {
        this.taxiService = taxiService;
    }

    @Resource
    private RedisUtil redisUtil;

    @ApiOperation("添加顺风车信息")
    @ApiResponses({
            @ApiResponse(code = 201,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @RequestMapping(value = "/taxi/add",method = RequestMethod.POST)
    public Map addTaxi(
            @RequestBody @ApiParam(value = "顺风车信息", required = true) Taxi taxi
    ){
        taxi.setCreated(new Date());
        taxi.setUpdated(new Date());
        int res = taxiService.insert(taxi);
        if(res > 0) {
            return Result.instance(201,"添加成功").toJsonMap();
        } else {
            return Result.instance(500,"添加失败").toJsonMap();
        }
    }

    @ApiOperation("api添加顺风车信息")
    @ApiResponses({
            @ApiResponse(code = 201,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @RequestMapping(value = "/api/taxi/add",method = RequestMethod.POST)
    public Map apiAddTaxi(
            @RequestBody @ApiParam(value = "顺风车信息", required = true) Taxi taxi
    ){
        taxi.setCreated(new Date());
        taxi.setUpdated(new Date());
        int res = taxiService.insert(taxi);
        if(res > 0) {
            return Result.instance(201,"添加成功").toJsonMap();
        } else {
            return Result.instance(500,"添加失败").toJsonMap();
        }
    }

    @ApiOperation("删除顺风车信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "删除成功"),
            @ApiResponse(code = 500,message = "删除失败")
    })
    @RequestMapping(value = "/taxi/del/{id}",method = RequestMethod.GET)
    public Map delTaxi(
            @PathVariable @ApiParam(value = "顺风车主键", required = true) Long id
    ){
        String key = "taxi#" + id.toString();
        if(redisUtil.hasKey(key)) {
            // 存在缓存 删除缓存
            redisUtil.del(key);
        }
        int res = taxiService.deleteByPrimaryKey(id);
        if(res > 0) {
            return Result.instance(204,"删除成功").toJsonMap();
        } else {
            return Result.instance(500,"删除失败").toJsonMap();
        }
    }

    @ApiOperation("更新顺风车信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/taxi/update",method = RequestMethod.POST)
    public Map updateTaxi(
            @RequestBody @ApiParam(value = "顺风车信息,必传主键", required = true) Taxi taxi
    ){
        String key = "taxi#" + taxi.getId().toString();
        if(redisUtil.hasKey(key)) {
            // 存在缓存 删除缓存
            redisUtil.del(key);
        }
        int res = taxiService.updateByPrimaryKeySelective(taxi);
        if(res > 0) {
            return Result.instance(204,"更新成功").toJsonMap();
        } else {
            return Result.instance(500,"更新失败").toJsonMap();
        }
    }

    @ApiOperation("api更新顺风车信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/api/taxi/update",method = RequestMethod.POST)
    public Map apiUpdateTaxi(
            @RequestBody @ApiParam(value = "顺风车信息,必传主键", required = true) Taxi taxi
    ){
        String key = "taxi#" + taxi.getId().toString();
        if(redisUtil.hasKey(key)) {
            // 存在缓存 删除缓存
            redisUtil.del(key);
        }
        int res = taxiService.updateByPrimaryKeySelective(taxi);
        if(res > 0) {
            return Result.instance(204,"更新成功").toJsonMap();
        } else {
            return Result.instance(500,"更新失败").toJsonMap();
        }
    }

    @ApiOperation("获取信息通过")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/taxi/findByTaxi",method = RequestMethod.POST)
    public Map findOneTaxi(
            @RequestBody @ApiParam(value = "顺风车信息", required = true) Taxi taxi
    ){
        Taxi taxi1 = taxiService.selectOne(taxi);
        if(taxi1 != null) {
            return Result.<Taxi>instance(200,"获取成功",taxi1).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取顺风车信息通过主键")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/taxi/findOneById/{id}",method = RequestMethod.GET)
    public Map findOneById(
            @PathVariable @ApiParam(value = "顺风车主键", required = true) Long id
    ){
        String key = "taxi#" + id.toString();
        if(redisUtil.hasKey(key)) {
            // 存在缓存
            Taxi taxi = (Taxi)redisUtil.get(key);
            if(taxi != null) {
                return Result.<Taxi>instance(200,"获取成功",taxi).toJsonMap();
            } else {
                return Result.instance(500,"获取失败").toJsonMap();
            }
        } else {
            // 不存在缓存
            Taxi taxi = taxiService.selectOneByPrimaryKey(id);
            if(taxi != null) {
                return Result.<Taxi>instance(200,"获取成功",taxi).toJsonMap();
            } else {
                return Result.instance(500,"获取失败").toJsonMap();
            }
        }
    }

    @ApiOperation("api获取顺风车信息通过主键")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/api/taxi/findOneById/{id}",method = RequestMethod.GET)
    public Map apiFindOneById(
            @PathVariable @ApiParam(value = "顺风车主键", required = true) Long id
    ){
        String key = "taxi#" + id.toString();
        if(redisUtil.hasKey(key)) {
            // 存在缓存
            Taxi taxi = (Taxi)redisUtil.get(key);
            if(taxi != null) {
                return Result.<Taxi>instance(200,"获取成功",taxi).toJsonMap();
            } else {
                return Result.instance(500,"获取失败").toJsonMap();
            }
        } else {
            // 不存在缓存
            Taxi taxi = taxiService.selectOneByPrimaryKey(id);
            if(taxi != null) {
                redisUtil.set(key,taxi,24*60*60);
                return Result.<Taxi>instance(200,"获取成功",taxi).toJsonMap();
            } else {
                return Result.instance(500,"获取失败").toJsonMap();
            }
        }
    }

    @ApiOperation("获取所有顺风车")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/taxi/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map selectAddressByTaxi(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "顺风车信息", required = true)  Taxi taxi){
        PageInfo<Taxi> pageInfo = taxiService.page(pageNum, pageSize, taxi);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

    @ApiOperation("api获取所有顺风车,需要填写出行分类：车找人或人找车")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/api/taxi/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map apiSelectByTaxi(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "顺风车信息,需要填写出行分类：车找人或人找车", required = true)  Taxi taxi){
        PageInfo<Taxi> pageInfo = taxiService.selectByTime(pageNum, pageSize, taxi);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

}
